<template>
  <div class="article-page">
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item>面经后台</el-breadcrumb-item>

      <el-breadcrumb-item>面经管理</el-breadcrumb-item>

    </el-breadcrumb>

    <el-card shadow="never" border="false">
      <!-- 插槽 -->
      <template #header>
        <div class="header">
          <span>共 {{ total }} 条记录</span>

          <el-button
            icon="el-icon-plus"
            size="small"
            type="primary"
            @click="openDrawer('add')"
            round>
            添加面经
          </el-button>

        </div>

      </template>
      <!-- 表格属性
      el-table：data是数据源 - 对象类型的数组
      el-table-column：列组件
      prop：数据源中的对象键名 - 填充数据
      label 列名
      width 宽度 -->
      <el-table :data="list" stripe style="width: 100%">

        <el-table-column prop="stem" label="标题" width="300"></el-table-column>
        <el-table-column prop="creator" label="作者" width="180"></el-table-column>
        <el-table-column prop="likeCount" label="点赞"> </el-table-column>
        <el-table-column prop="views" label="浏览数"> </el-table-column>
        <el-table-column prop="createdAt" label="更新时间"> </el-table-column>
        <el-table-column prop="createdAt" label="操作">
          <template #default="{ row }">
            <div class="actions">
              <i @click="openDrawer('preview', row.id)" class="el-icon-view"></i>

              <i @click="openDrawer('edit', row.id)" class="el-icon-edit-outline"></i>

              <i class="el-icon-delete" @click="del(row.id)"></i>

            </div>
          </template>
        </el-table-column>

      </el-table>
      <el-pagination
      background
      @current-change="handleCurrentChange"
      :current-page="current"
      :page-size="pageSize"
      layout="prev, pager, next"
      :total="total"></el-pagination>

    </el-card>
    <!-- :visible 控制显示隐藏
    :direction="direction"  控制方向 -->
    <el-drawer
    :visible="isShowDrawer"
    @update:visible="isShowDrawer = $event"
    :before-close="handleClose"
    :title="darwerTitle"
    size="60%"
    >
    <div v-if="drawerType === 'preview'" class="article-preview">
      <h5>{{ form.stem }}</h5>

      <div v-html="form.content"></div>

    </div>
    <el-form ref="form" :rules="rules" :model="form" label-width="80px">
      <el-form-item label="标题" prop="stem">
        <el-input v-model="form.stem"  placeholder="输入面经标题"></el-input>

      </el-form-item>

      <el-form-item label="内容" prop="content">
        <!-- 由于富文本的插件不属于element-ui的内部组件(必须手动校验)
        表单组件实例.validateField('校验的字段') -->
        <quill-editor v-model="form.content" @blur="$refs.form.validateField('content')"></quill-editor>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="submit">确认</el-button>

        <el-button @click="handleClose">取消</el-button>

      </el-form-item>

    </el-form>

    </el-drawer>
  </div>

</template>

<script>
import { createArticle, getArticleDetail, getArticleList, removeArticle, updateArticle } from '@/api/article'
// require styles
import 'quill/dist/quill.core.css'
import 'quill/dist/quill.snow.css'
import 'quill/dist/quill.bubble.css'
import { quillEditor } from 'vue-quill-editor'
export default {
  name: 'article-page',
  components: {
    quillEditor
  },

  data () {
    return {
      list: [],
      rules: {
        stem: [{ required: true, message: '请输入面经标题', trigger: 'blur' }],
        content: [{ required: true, message: '请输入面经标题', trigger: 'blur' }]
      },
      current: 1,
      pageSize: 10,
      total: 0,
      isShowDrawer: false,
      darwerType: '',
      form: {
        stem: '', // 标题
        content: '' // 内容
      }
    }
  },
  created () {
    this.initTableList()
  },
  methods: {
    // 确认-文章添加/编辑
    async submit () {
    // 表单的整体校验
      await this.$refs.form.validate()
      // 如何区分, 当前是 编辑 还是 添加
      if (this.drawerType === 'add') {
      // 发送请求
        await createArticle(this.form)
        // 添加提示 $message.success
        this.$message.success('添加成功')
      }
      if (this.drawerType === 'edit') {
      // 发送的是编辑的请求
        const { id, stem, content } = this.form
        await updateArticle({ id, stem, content })
        this.$message.success('修改成功')
      }
      // 重新渲染 - 重置为第一页
      this.current = 1
      this.initTableList()
      this.handleClose()
    },
    async initTableList () {
      // 解构赋值：快速的获取想要的值
      const { data: { rows, total } } = await getArticleList({
        current: this.current,
        pageSize: this.pageSize
      })
      this.list = rows
      this.total = total
    },
    // 切换页
    handleCurrentChange (val) {
      console.log(`当前页：${val}`)
      // 1. 当前页的更新
      this.current = val
      // 2. 重新加载
      this.initTableList()
    },
    async del (id) {
      // 删除请求
      await removeArticle(id)
      // 添加成功提示
      this.$message.success('删除成功')
      // 处理删除当前页的最后一条
      if (this.list.length === 1 && this.current > 1) {
        this.current--
      }
      // 重新渲染
      this.initTableList()
    },
    // 添加/预览/编辑
    async openDrawer (type, id) {
      this.darwerType = type
      this.isShowDrawer = true
      // 判断执行的操作不是添加 -> 根据文章id > 文章信息
      if (type !== 'add') {
        const res = await getArticleDetail(id)
        console.log('res', res)
        this.form = {
          ...res.data
        }
      }
    },
    handleClose (done) {
      // 额外操作：重置除了标题和内容的其他字段
      this.form = {
        stem: '',
        content: ''
      }
      // 把关闭和重置功能 封装到单独的方法中
      // 注意点: 由于公用的抽屉, 当预览时, 是没有表单的! 不能重置表单
      if (this.drawerType !== 'preview') {
        // add edit 调用 resetFields 在此处的作用: 重置校验状态
        this.$refs.form.resetFields()
      }
      this.isShowDrawer = false
    }
  },
  computed: {
    darwerTitle () {
      let title = ''
      if (this.darwerType === 'add') title = '添加面经'
      if (this.darwerType === 'preview') title = '预览面经'
      if (this.darwerType === 'edit') title = '编辑面经'
      return title
    }
  }
}
</script>

<style lang="scss" scoped>
.el-card {
  margin-top: 25px;
  .header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding-right: 16px;
  }
  .actions {
    font-size: 18px;
    display: flex;
    justify-content: space-around;
    color: #666;
    > i:hover {
      color: rgba(114, 124, 245, 1);
      cursor: pointer;
    }
  }
}
.el-pagination {
  margin-top: 20px;
  text-align: center;
}
.el-breadcrumb {
  margin-top: 10px;
}
.el-form {
  padding-right: 40px;
}
.quill-editor {
  ::v-deep .ql-editor {
    height: 300px;
  }
}
.el-rate {
  padding: 10px 0;
}
.article-preview {
  padding: 0 40px 40px 40px;
  > h5 {
    font-size: 20px;
    color: #666;
    border-bottom: 1px dashed #ccc;
    padding-bottom: 30px;
    margin: 0 0 20px 0;
  }
}
</style>
